package com.mystudy.leetcode.contest.p_5130;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/21 10:34
 * @Description 多骨诺米牌的数量
 */
public class Solution {
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);
    long startTime = 0L;
    long endTime = 0L;
    @Before
    public void before() {
        startTime = System.nanoTime();
    }

    @Test
    public void test() {
        int i = numEquivDominoPairs(new int[][]{{1, 2}, {1, 2}, {1, 1}, {1, 2}, {2, 2}});
        LOGGER.debug("i = [{}]", i);

    }

    @After
    public void after() {
        endTime = System.nanoTime();
        double cost = (endTime - startTime) / 1000000.0;
        LOGGER.debug("cost = [{} ms ]",cost);
    }


    public int numEquivDominoPairs(int[][] dominoes) {
        int[] res = new int[100];
        for (int i = 0; i < dominoes.length; i++) {
            if (dominoes[i][0] > dominoes[i][1]) {
                res[dominoes[i][0] * 10 + dominoes[i][1]]++;
            } else {
                res[dominoes[i][1] * 10 + dominoes[i][0]]++;
            }
        }
        LOGGER.debug("res = [{}]", res);
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            if (res[i] > 1) {
                LOGGER.debug("res[i] = [{}]", res[i]);
                //说明有成对的出现，其排列组合为C(i)(2)，也就是i*i-1/2*1
                LOGGER.debug("sum = [{}]", sum);
                LOGGER.debug("res[i]*res[i]-1 = [{}]", res[i] * res[i] - 1);
                sum = sum + (res[i] * (res[i] - 1) / 2);
                LOGGER.debug("sum = [{}]", sum);
            }
        }
        return sum;
    }

}
